package com.example.himalayaproject.find.broadcast.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.broadcast.bean.TopRadios;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/12/5.
 */

public class TopAdapter extends RecyclerView.Adapter<TopAdapter.ViewHolder> {
    private Context context;
    private List<TopRadios> tops;
    private int TAG = 1;

    public TopAdapter(Context context) {
        this.context = context;
        tops = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(viewType, parent, false));
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        if (tops.size()==3) {
            TAG = 0;
            return R.layout.broadcast_item;
        } else {
            return R.layout.bct_top_item;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (tops.size()==3 && TAG==0) {
            TopRadios topRadios = tops.get(position);
            holder.nameTextView.setText(topRadios.getName());
            if (topRadios.getProgramName() == null){
                holder.currTextView.setText("暂无节目单");
            } else {
                holder.currTextView.setText("正在直播："+topRadios.getProgramName());
            }
            float count = ((float) Integer.parseInt(topRadios.getPlayCount()))/10000;
            holder.numberTextView.setText(count+"万人");
            Picasso.with(context).load(topRadios.getCoverSmall()).into(holder.srcImage);
        }
        else {
                if (position == 0){
                    holder.rankTextView.setTextColor(Color.RED);
                } else if (position == 1){
                    holder.rankTextView.setTextColor(Color.parseColor("#ee9a00"));
                } else if (position == 2){
                    holder.rankTextView.setTextColor(Color.GREEN);
                }
                holder.rankTextView.setText(position+1+"");
        }
    }



    public void addAll (Collection<? extends TopRadios> collection) {
        int size = tops.size();
        tops.addAll(collection);
        notifyItemRangeInserted(size,collection.size());
    }


    @Override
    public int getItemCount() {
        return tops.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTextView,currTextView,numberTextView,rankTextView;
        private ImageView imageView,srcImage,albumImage;
        public ViewHolder(View itemView) {
            super(itemView);
            nameTextView = ((TextView) itemView.findViewById(R.id.bct_item_name));
            currTextView = ((TextView) itemView.findViewById(R.id.bct_item_current));
            numberTextView = ((TextView) itemView.findViewById(R.id.bct_item_number));
            rankTextView = ((TextView) itemView.findViewById(R.id.bct_top_rank));
            imageView = ((ImageView) itemView.findViewById(R.id.bct_item_play));
            srcImage = ((ImageView) itemView.findViewById(R.id.bct_item_src));
            albumImage = ((ImageView) itemView.findViewById(R.id.bct_item_album_play));
        }
    }
}
